import pygame


class Ship:
    def __init__(self, screen, ai_settings):
        """要显示的屏幕"""
        self.screen = screen

        """获取图片和屏幕的矩形，把不规则图片转为处理矩形"""
        """首先加载图片"""
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = self.screen.get_rect()

        """设置飞船位置为底部中间"""
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        self.moving_right = False
        self.moving_left = False

        """为了使飞船能移动小数像素，但是centerx只能保存整数，所以设置一个小数保存移动后的位置"""
        self.centerx = float(self.rect.centerx)
        self.ai_settings = ai_settings

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.centerx += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.centerx -= self.ai_settings.ship_speed_factor
        self.rect.centerx = self.centerx

    def center_ship(self):
        self.rect.centerx = self.screen_rect.centerx
